package interfaces.clients.storage;

import interfaces.clients.rightscontrol.Blocks;
import interfaces.clients.rightscontrol.UsersRights;

import java.io.*;

/**
 * Created by Chwan on the 28.11.2006 at 22:44:48
 */
public class Properties implements Serializable {
	File file;

	/*public Properties(File file) {
		this.file = file;
	}

	public Properties(String filename) {
		file = new File(filename);
	}*/

	private Blocks blocks;
	private UsersRights usersRights;
	private String tmp;

	public void setTmp(String tmp) {
		this.tmp = tmp;
	}

	public String getTmp() {
		return tmp;
	}

	public void setBlocks(Blocks blocks) {
		this.blocks = blocks;
	}

	public void setUsersRights(UsersRights usersRights) {
		this.usersRights = usersRights;
	}

	public Blocks getBlocks() {
		return blocks;
	}

	public UsersRights getUsersRights() {
		return usersRights;
	}

	public void writeToFile(String filename) {
		//TODO: it is bad. try to get info from repos
		writeIntoFile(new File("c:\\temp\\repos\\props", filename));
	}

/*	public void witeToFile(File f) {
		writeIntoFile(new File("c:\\temp\\repos\\props", f.get));
	}
*/

	private void writeIntoFile(File f) {
		ExtendedFile file = new ExtendedFile(f, "properties");
		if(!file.exists()) {
			try {
				file.createFile();
			} catch (StorageIOException e) {
				//System.out.println(e.getMessage());
			}
		}
		OutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			try {
				throw new StorageFileNotFoundException(file.getPath()+" not found!", e);
			} catch (StorageFileNotFoundException e1) {
				//System.out.println(e1.getMessage());
			}
		}
		try {
			this.writeTo(fos);
		} catch (StorageIOException e) {
			//System.out.println(e.getMessage());
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	public void writeTo(OutputStream os) throws StorageIOException {
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(os);
		} catch (IOException e) {
			throw new StorageIOException("Properties.writeTo has some IO problems!", e);
		}
		try {
			oos.writeObject(this);
			oos.flush();
		} catch (IOException e) {
			throw new StorageIOException("Properties.writeTo has some IO problems!", e);
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				throw new StorageIOException("Properties.writeTo has some IO problems!", e);
			}
		}
	}

	public void readFromFile(String filename) {
		//TODO: it is bad. try to get info from repos
		readFromInsideOfFile(new File("c:\\temp\\repos\\props", filename));
	}

/*	public void readFromFile(File f) {
		readFromInsideOfFile(f);
	}
*/

	private void readFromInsideOfFile(File f) {
		ExtendedFile file = new ExtendedFile(f, "properties");
		InputStream fis = null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			try {
				throw new StorageFileNotFoundException(f.getPath()+" not found!", e);
			} catch (StorageFileNotFoundException e1) {
				//System.out.println(e1.getMessage());
			}
		}

		try {
			Properties p = this.readFrom(fis);
			this.setBlocks(p.getBlocks());
			this.setTmp(p.getTmp());
			this.setUsersRights(p.getUsersRights());
		} catch (StorageIOException e) {
			//System.out.println(e.getMessage());
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	public Properties readFrom(InputStream is) throws StorageIOException {
		ObjectInputStream ois;
		try {
			ois = new ObjectInputStream(is);
		} catch (IOException e) {
			throw new StorageIOException("Properties.readFrom has some IO problems!", e);
		}
		Properties prop = null;
		try {
			prop = (Properties) ois.readObject();
		} catch (IOException e) {
			throw new StorageIOException("Properties.writeTo has some IO problems!", e);
		} catch (ClassNotFoundException e) {
			throw new StorageIOException("Properties.writeTo has some IO problems!", e);
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				throw new StorageIOException("Properties.writeTo has some IO problems!", e);
			}
		}
		return prop;
	}
}
